package com.ocom.payhandle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfRecordProcess;
import com.ocom.common.entity.card.EntityXfRecordWaitpay;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.vo.pyhandle.WaitPayListVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payhandle.dao.IDaoXfRecordWaitpay;
import com.ocom.payhandle.service.XfRecordProcessService;
import com.ocom.payhandle.service.XfRecordWaitpayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class XfRecordWaitpayServiceImpl extends ServiceImpl<IDaoXfRecordWaitpay, EntityXfRecordWaitpay> implements XfRecordWaitpayService {

    @Autowired
    IDaoXfRecordWaitpay iDaoXfRecordWaitpay;

    @Autowired
    XfRecordProcessService xfRecordProcessService;

    @Override
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean saveXfRecordWaitpay(Long comId, EntityXfRecordWaitpay entityXfRecordWaitpay) {
        return this.save(entityXfRecordWaitpay);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean saveXfRecordWaitpay2(Long comId, EntityXfRecordWaitpay entityXfRecordWaitpay, EntityXfRecordProcess updateXfRecordProcess) {
        boolean bf = this.save(entityXfRecordWaitpay);
        if (!bf) return false;
        int upSuc = xfRecordProcessService.updateEntityXfRecord(comId, updateXfRecordProcess);
        if (upSuc <= 0) return false;
        return true;
    }

    @Override
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean updateXfRecordWaitpay(Long comId, Long id , Integer status) {
        UpdateWrapper<EntityXfRecordWaitpay> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.eq("com_id",comId);
        updateWrapper.set("pay_state",status);
        if(status==1){
            updateWrapper.set("pay_time",new Date());
        }
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(companyId = "comId", type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfRecordWaitpay getRepairPaySum(Long comId, Long pId) {
        QueryWrapper<EntityXfRecordWaitpay> queryWrapper = Wrappers.query();
        queryWrapper.select("com_id," +
                " p_id," +
                " p_name," +
                " p_code," +
                " dept_name," +
                " DATEDIFF(now(),  min(create_time)) days," +
                " sum(0 - real_money) amount," +
                " sum(1) times");
        queryWrapper.eq("com_id", comId);
        queryWrapper.in("pay_state", -1,2);
        queryWrapper.eq("p_id", pId);
        queryWrapper.groupBy("p_id");
        queryWrapper.orderByDesc("p_id");
        return this.getOne(queryWrapper);
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<WaitPayListVo> getXfRecordWaitpay(Long companyId, Long pId) {
        return iDaoXfRecordWaitpay.selectWaitpay(companyId,pId);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityXfRecordWaitpay getXfRecordWaitpayInfo(Long companyId, Long id) {
        QueryWrapper<EntityXfRecordWaitpay> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.eq("id", id);
        return this.getOne(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean updateXfRecordWaitpayById(Long companyId, EntityXfRecordWaitpay entityXfRecordWaitpay) {
        return this.updateById(entityXfRecordWaitpay);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public Boolean updateXfRecordWaitpay(Long companyId, String orderNo, Date payTime) {
        UpdateWrapper<EntityXfRecordWaitpay> updateWrapper =new UpdateWrapper<>();
        updateWrapper.eq("order_no",orderNo);
        updateWrapper.eq("com_id",companyId);
        updateWrapper.eq("pay_state",0);
        updateWrapper.set("pay_state",1);
        updateWrapper.set("pay_time",payTime);
        return this.update(updateWrapper);
    }

}
